package dbgroup02.validator;

import java.util.regex.Pattern;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import dbgroup02.bean.PassengerSelection;

public class PassengerSelectionValidator implements Validator {

	@Override
	public boolean supports(Class clazz) {
		return PassengerSelection.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object obj, Errors errs) {
		PassengerSelection passengerSelection = (PassengerSelection) obj;
		
		if (passengerSelection.getLastName().length() == 0) 
			errs.rejectValue("lastName", "lastName.missing", null, "Last name cannot be empty");
		else if (!Pattern.matches("[A-Za-z\\-]{2,50}", passengerSelection.getLastName())) {
			errs.rejectValue("lastName", "lastName.invalid", null, "Invalid last name");
		}
		
		if (passengerSelection.getIdTicket().length() == 0) 
			errs.rejectValue("idTicket", "idTicket.missing", null, "Ticket ID cannot be empty");
		else if (!Pattern.matches("[0-9]{1,5}", passengerSelection.getIdTicket())) {
			errs.rejectValue("idTicket", "idTicket.invalid", null, "Invalid Ticket ID");
		}
		
	}	
}
